/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradeplus.jms;

import com.tradeplus.admin.service.AdminService;
import com.tradeplus.security.model.User;
import com.tradeplus.security.service.UserDetailsService;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 *
 * @author ronnie
 */
public class EmailServiceImpl implements EmailService{

    private AdminService adminService;
    private UserDetailsService userDetailsService;

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
    
    public String getAppUrl() {
	return adminService.getValueForSystemProperty("liveUrl");
    }
    
    
    /**
     * Send html email
     * 
     * @param fromEmail
     * @param recipientEmails
     * @param copiedRecipientEmails
     * @param blindRecipientEmails
     * @param subjectLine
     * @param bodyText
     * @param htmlText
     * @param attachments
     * @param inLineAttachments
     * @throws MessagingException
     */
    @Override
    @SuppressWarnings("unchecked")
    public void sendEmail(String fromEmail, List<String> recipientEmails,
    		List<String> copiedRecipientEmails, List<String> blindRecipientEmails,
    		String subjectLine, String bodyText, String htmlText, List attachments,
    		List inLineAttachments, boolean isHtml) throws MessagingException {

    	boolean hasAtLeastOneRecipient = false;
    	
    	String smtpHost = adminService.getValueForSystemProperty("smtpHost");

    	if(smtpHost != null && smtpHost.length() > 0) {
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost(smtpHost);

            MimeMessage email = mailSender.createMimeMessage();
            boolean multipart = true;
            MimeMessageHelper helper = new MimeMessageHelper(email, multipart, "UTF-8");
            helper.setFrom(fromEmail);

            if(recipientEmails != null && recipientEmails.size() > 0) {
                hasAtLeastOneRecipient = true;
                String[] recipientEmailArr = (String[]) recipientEmails.toArray(new String[1]);
                helper.setTo(recipientEmailArr);
            }

            if(copiedRecipientEmails != null && copiedRecipientEmails.size() > 0) {
                hasAtLeastOneRecipient = true;
                String[] copiedRecipientEmailArr = (String[]) copiedRecipientEmails
                .toArray(new String[1]);
                helper.setCc(copiedRecipientEmailArr);
            }

            if(blindRecipientEmails != null && blindRecipientEmails.size() > 0) {
                hasAtLeastOneRecipient = true;
                String[] blindRecipientEmailArr = (String[]) blindRecipientEmails
                .toArray(new String[1]);
                helper.setBcc(blindRecipientEmailArr);
            }

            if(subjectLine != null && subjectLine.length() > 0) {
                helper.setSubject(subjectLine);
            }

            if(htmlText != null && htmlText.length() > 0) {
                helper.setText(htmlText, isHtml);
            } else {
                if(bodyText != null && bodyText.length() > 0) {
                    helper.setText(bodyText);
                }
            }
            if(hasAtLeastOneRecipient) {
                mailSender.send(email);
            }
    	}
    }

    @Override
    public Boolean SendTestEmail(String emailID, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean SendNotificationEmail(int userId, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean SendPanelistRegistrationRespondEmail(int activationCode, int userId, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean SendPanelistCreateConfirmationEmail(int jobID, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean sendPasswordRequestEmail(String emailAddress, User user, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean sendCustomerRespondEmail(int userId, ServletContext ctx) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean SendCustomerCreateConfirmationEmail(int jobID, ServletContext ctx) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean sendSupportEmail(String from, String emailSupport, String userName, ServletContext ctx) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
